{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import nltk\n",
    "import random\n",
    "\n",
    "file = open('Text/Walden.txt', 'r')\n",
    "walden = file.read()\n",
    "walden = walden.split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def makePairs(arr):\n",
    "    pairs = []\n",
    "    for i in range(len(arr)):\n",
    "        if i < len(arr)-1: \n",
    "            temp = (arr[i], arr[i+1])\n",
    "            pairs.append(temp)\n",
    "    return pairs\n",
    "\n",
    "def generate(cfd, word = 'the', num = 50):\n",
    "    for i in range(num):\n",
    "        arr = []                                      # make an array with the words shown by proper count\n",
    "        for j in cfd[word]:\n",
    "            for k in range(cfd[word][j]):\n",
    "                arr.append(j)\n",
    "        \n",
    "        print(word, end=' ')\n",
    "        word = arr[int((len(arr))*random.random())]     # choose the word randomly from the conditional distribution\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the question, perhaps from the leisure fruitful. \"But,\" says pertinently that sound asleep thus. Yet his gentle rain while to choose. If he was nearly level, and wisely; as he sprang upon the entrance of goldenrod (Solidago stricta) grows a half-hour's nap after practising various animals of some cheerful it "
     ]
    }
   ],
   "source": [
    "pairs = makePairs(walden)\n",
    "cfd = nltk.ConditionalFreqDist(pairs)\n",
    "generate(cfd)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "temperature of the farm; but go to hand, had made. It is not necessary to be translated before I do not see that it took many eagles about it, and fuel. Instead of the mildness and bed even with a napkin to the hearer, else whom was not mainly, who "
     ]
    }
   ],
   "source": [
    "# I'm gonna make a method so I can do it a bunch of times really easily.\n",
    "\n",
    "def makeText(fileName, word = 'the', num = 50):\n",
    "    file = open(fileName, 'r')\n",
    "    string = file.read()\n",
    "    stringArr = string.split()\n",
    "    \n",
    "    pairs = makePairs(stringArr)\n",
    "    model = nltk.ConditionalFreqDist(pairs)\n",
    "    generate(model, word, num)\n",
    "    \n",
    "makeText('Text/Walden.txt', 'temperature')                 # sweet. Now all I have to do is input a file name."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Thank you. Thank you. God bless the clean energy in New Hampshire and the results of Scripture, the comparison was a band of funding the worst of honorable men and rural communities to proclaim an equal day's work. And that's a nation that my new jobs overseas and discord. On "
     ]
    }
   ],
   "source": [
    "# In the same directory are text files of Thus Spoke Zarathustra by Nietzsche and some speeches by Obama.\n",
    "makeText('Text/Obama.txt', 'Thank')     # he usually starts his speeches with 'Thank you'."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
